package fr.cephb.socialsnp.webapp;

import java.sql.ResultSet;
import java.sql.SQLException;

public class NamedId<T extends Comparable<T>>
	implements Comparable<NamedId<T>>
	{
	public static class Long
		extends NamedId<java.lang.Long>
		{
		public Long(long id, String name)
			{
			super(id,name);
			}
		
		public Long(ResultSet row) throws SQLException
			{
			this(row.getLong(1),row.getString(2));
			}
		
		
		}
	
	private T id;
	private String name;
	public NamedId(T id,String name)
		{
		this.id=id;
		this.name=name;
		if(this.name==null) this.name=String.valueOf(id);
		}
	
	public T getId() {
		return id;
		}
	
	public String getName() {
		return name;
		}
	
	@Override
	public int hashCode() {
		return getId().hashCode();
		}
	
	@Override
	public boolean equals(Object obj) {
		if(obj==this) return true;
		if(obj==null || getClass()!=obj.getClass()) return false;
		return getId().equals(NamedId.class.cast(obj).getId());
		}
	
	@Override
	public int compareTo(NamedId<T> o)
		{
		return getName().compareToIgnoreCase(o.getName());
		}
	
	@Override
	public String toString() {
		return "("+getId()+"; "+getName()+")";
		}
	}
